582A - GCD Table - CodeForces Solution


constructive algorithms greedy number theory *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b){
    return !b ? a : gcd(b, a%b);
}
int kor(int n){
    int q = sqrt(n);
    return q + ((q+1)*(q+1) == n);
}

int main(){
    int n; cin >> n;
    int n2 = n*n, p = n2;
    int a[n2];
    for(int i=0; i < n2; i++) cin >> a[i];
    sort(a, a+n2);
    vector<int> b;
    map<int, int> m;
    for(int i = n2-1; i >= 0; i--){
        if(i && a[i] == a[i-1]) continue;
        for(int k = p-i; k > m[a[i]]; k--){
            for(int t : b) m[gcd(a[i], t)] += 2;
            b.push_back(a[i]);
        }
        p=i;
    }
    for(int t : b) cout << t << ' ';
    return 0;
}


Comments

Submit
0 Comments
More Questions

13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division